package com.app.llpb;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.app.llpb.model.LLPBInfo;
import com.app.llpb.util.LLPBController;
import com.app.llpb.util.LogEx;
import com.app.llpb.widget.LinearLayoutProgressBar;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.PropertyValuesHolder;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
import com.nineoldandroids.view.ViewHelper;

/**
 * 线性进度动画
 * Author：cjj
 * Blog：jianjunchen.net
 */
public class LLPBAndText extends FrameLayout {

    private View mainView;
    private TextView tv_pb, end;
    private LinearLayoutProgressBar llpb;
    private View pb_line;

    private LLPBInfo llpnInfo;
    private int width;

    public LLPBAndText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    public LLPBAndText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LLPBAndText(Context context) {
        this(context, null, 0);
    }

    private void init(Context context, AttributeSet attrs) {
        initView(context);
        ViewTreeObserver vto2 = getViewTreeObserver();
        vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeGlobalOnLayoutListener(this);
                width = llpb.getMeasuredWidth();
                postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startAnimator();
                    }
                }, LLPBController.ANIMATOR_DELAY);

            }
        });
    }

    private void initView(Context context) {
        mainView = LayoutInflater.from(context).inflate(R.layout.llpbandtv, null);
        removeAllViews();
        addView(mainView);
        tv_pb = (TextView) mainView.findViewById(R.id.tv_pb);
        llpb = (LinearLayoutProgressBar) mainView.findViewById(R.id.first_pb);
        end = (TextView) mainView.findViewById(R.id.end);
        pb_line = mainView.findViewById(R.id.pb_line);
    }

    /**
     * 设置控件进度条颜色
     *
     * @param color
     */
    public void setPbColor(int color) {
        if (null == llpb) {
            return;
        }
        llpb.setPbBackgroungColor(color);
        if (null == mainView) {
            return;
        }
        ((TextView) mainView.findViewById(R.id.initial)).setTextColor(getResources().getColor(color));
        pb_line.setBackgroundColor(getResources().getColor(color));
        end.setTextColor(getResources().getColor(color));

    }

    /**
     * 设置默认进度颜色
     */
    public void setDefaultColor(int color) {
        if (null == mainView) {
            return;
        }
        mainView.findViewById(R.id.second_pb).setBackgroundColor(getResources().getColor(color));
    }

    /**
     * 更新数据
     *
     * @param llpnInfo
     */
    public void setData(LLPBInfo llpnInfo) {
        if (null == llpnInfo) {
            return;
        }
        this.llpnInfo = llpnInfo;
        llpb.setPb(llpnInfo.getPb());
        llpb.setTotle(llpnInfo.getTotle());
        String totle = (int) llpnInfo.getTotle() + "";
        tv_pb.setText(totle);
        end.setText(totle);

    }

    /**
     * 启动动画
     */
    public void startAnimator() {
        float pb_wid = llpnInfo.getPb() * width / llpnInfo.getTotle();
        pbAnimator(width, pb_wid, 0, pb_wid - width, (int) llpnInfo.getTotle(), (int) llpnInfo.getPb()).start();
    }

    /**
     * 设置进度
     *
     * @param pb
     */
    public void setPb(float pb) {
        llpb.setPb(pb);
        llpb.invalidate();
    }

    /**
     * 位移动画
     */
    public Animator pbAnimator(float pb_start, float pb_end, float tv_start, float tv_end, int txt_start, int txt_end) {
        PropertyValuesHolder pvh_translation = PropertyValuesHolder.ofFloat("translationX", tv_start, tv_end);
        PropertyValuesHolder pvh_pb = PropertyValuesHolder.ofFloat("pb", pb_start, pb_end);
        PropertyValuesHolder pvh_txt = PropertyValuesHolder.ofInt("txt", txt_start, txt_end);
        ValueAnimator animator = ValueAnimator.ofPropertyValuesHolder(pvh_translation, pvh_pb, pvh_txt);
        animator.setDuration(LLPBController.ANIMATION_DURATION);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(new AnimatorUpdateListener() {
            @SuppressLint("NewApi")
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float translationX = (Float) animation.getAnimatedValue("translationX");
                ViewHelper.setTranslationX(tv_pb, translationX);
                tv_pb.setText(animation.getAnimatedValue("txt") + "");
                ViewHelper.setTranslationX(pb_line, translationX);
                float pb = (Float) animation.getAnimatedValue("pb");
                LogEx.d("llpnInfo", "llpnInfo:" + pb);
                setPb(pb);
            }
        });
        return animator;
    }

}
